package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {


  @Autowired
  private StringRedisTemplate stringRedisTemplate;
  @Autowired
  private CustomerInfoFeignClient customerInfoFeignClient;
    @Override
    public String login(String code) {
        //远程调用service中的登录业务接口完成微信登录
      Result<Long> longResult = customerInfoFeignClient.login(code);
      //2 判断如果返回失败了，返回错误提示
      Integer codeResult = longResult.getCode();
      if(codeResult != 200) {
        throw new GuiguException(ResultCodeEnum.DATA_ERROR);
      }
      Long userId = longResult.getData(); //获取用户id
      //生成token
      String token = UUID.randomUUID().toString().replaceAll("-", "");
      String key = RedisConstant.USER_LOGIN_KEY_PREFIX+token;
      //将用户信息存入Redis中并设置过期时间
      stringRedisTemplate.opsForValue().
              set(key,userId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.MINUTES);
      //返回token
      return token;
    }

  /**
   * 用户登录信息查询
   *
   * @param userId
   * @return
   */
  @Override
  public CustomerLoginVo getCustomerLoginVoById(Long userId) {
    //远程调用service获取用户信息
    if (!Optional.ofNullable(userId).isPresent()) {
      throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
    }
    Result<CustomerLoginVo> result =  customerInfoFeignClient.getCustomerLoginVo(userId);
    if (result.getCode() != 200) {
      throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
    }
    if (result.getData() == null) {
      throw new GuiguException(ResultCodeEnum.DATA_ERROR);
    }
    return result.getData();
  }

  /**
   * 根据微信code获取微信手机号并更新用户手机号
   *
   * @param updateWxPhoneForm
   * @return
   */
  @Override
  public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
    /*if (!StringUtils.hasText(updateWxPhoneForm.getCode())) {
      throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
    }*/
    Long userId = AuthContextHolder.getUserId();
    updateWxPhoneForm.setCustomerId(userId);
    Result<Boolean> result =  customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
    return result.getCode() == 200;
  }
}
